package com.tianquan.miracle.context;/**
 * @author starsea
 * @date 2024-09-29 上午10:33
 */

import com.tianquan.miracle.annotation.EnableAutoConfiguration;
import com.tianquan.miracle.annotation.MiracleFastApplication;
import com.tianquan.miracle.beans.ScannerClassLoad;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @BelongsProject: uploaddemo
 * @BelongsPackage: com.tianquan.miracle.context
 * @Author: tianquan
 * @CreateTime: 2024-09-29  10:33
 * @Description: 默认上下文容器
 * @Version: 1.0
 */
public class DefaultApplicationContext extends GeneralApplicationContext {

    private final ScannerClassLoad scannerClass = new ScannerClassLoad();

    private Class<?> clazz;

    private final String[] args;

    private Boolean isEnabled = false;

    public Boolean isEnabled() {
        return isEnabled;
    }

    public List<Class<?>> findClassesInPackageAndSubpackages() {
        return loadClass(clazz);
    }

    public Set<String> extendPreBeanPostProcessor() {
        return new HashSet<>();
    }

    public List<Class<?>> loadClass(Class<?> clazz) {
        return loadClass(clazz.getPackageName());
    }

    public List<Class<?>> loadClass(String packageName) {
        return scannerClass.loadClass(packageName);
    }

    public DefaultApplicationContext(Class<?> clazz, String[] args) {
        LookUpIsEnableAutoConfiguration(clazz);
        this.clazz = clazz;
        this.args = args;
    }

    public void LookUpIsEnableAutoConfiguration(Class<?> clazz) {
        // 是否开启自动配置类
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation instanceof EnableAutoConfiguration enableAutoConfiguration ) {
                isEnabled = enableAutoConfiguration.value();
                return;
            }
            if (annotation instanceof MiracleFastApplication miracleFastApplication) {
                isEnabled = true;
            }
        }
    }

}
